﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace ArchSoft
{
    public partial class IssuedFrm : Form
    {
        string FolList = "01 Client";
        string CurDir = "";
        Thread myth = null;
        ProgressingWin fr = null;
        public string ListOfFolderNedIss
        {
            get { return FolList; }
        }

        public string ProjectFolder
        {
            set { CurDir = value; }
        }

        public IssuedFrm(bool CopyXrefable)
        {
            InitializeComponent();
            this.Ra_Xf.Enabled = CopyXrefable;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtDes.Text == "")
            {
                //MessageBox.Show("Please fill in description field", "Issued", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show("Vui lòng nhập vào phần mô tả", "Phát hành", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            foreach (RadioButton bx in this.groupBox1.Controls)
            {
                if (bx.Checked)
                {
                    FolList = bx.Tag.ToString();
                    break;
                }
            }

            if (this.Chx_DWG.Checked)
            {
                //Issued!
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                //Copy Xref!!!
                myth = new Thread(new ThreadStart(CopyIn));
                myth.IsBackground = true;
                fr = new ProgressingWin(myth);
                //fr.MessText = "None";
                //fr.Text = "Issued Xref";
                fr.MessText = "Không";
                fr.Text = "Phát hành Xref";
                this.Opacity = 0;
                fr.ShowDialog();

                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        #region " For  Copy Xref! "

        string GetDate()
        {
            DateTime dateTime = DateTime.Now;
            return dateTime.Year + "." + dateTime.Month.ToString("00") + "." + dateTime.Day.ToString("00");
        }

        private string GetUniqueName(string mainName, string StoreDir)
        {
            int order = 0;
            string result = mainName;

            while (Directory.Exists(StoreDir + @"\" + result))
            {
                order++;
                result = mainName + "(" + order + ")";
            }
            return result;
        }

        void CopyIn()
        {
            try
            {
                foreach (RadioButton bx in this.groupBox1.Controls)
                {
                    if (bx.Checked)
                    {
                        string CpFrom = CurDir + Path.DirectorySeparatorChar + ProjectManager.PROJECT_OUTPUT_FOLDER + "\\Xref";
                        //string CpHere = CurDir + Path.DirectorySeparatorChar + ProjectManager.PROJECT_ISSUED_FOLDER + Path.DirectorySeparatorChar + bx.Tag.ToString() + "\\Xref";
                        //CpHere = CpHere + Path.DirectorySeparatorChar + GetUniqueName(GetDate(), CpHere) + "_" + txtDes.Text;

                        /*Doi lai thu muc dang YYMMDD_Xref_desc ko con la Xref\\xxx*/
                        string CpHere = CurDir + Path.DirectorySeparatorChar + ProjectManager.PROJECT_ISSUED_FOLDER + Path.DirectorySeparatorChar + bx.Tag.ToString();
                        CpHere = CpHere + Path.DirectorySeparatorChar + GetUniqueName(CodeNameUtilities.GetDateYYMMDD(DateTime.Now), CpHere) + "_Xref_" + txtDes.Text;
                        MoveDirectory(CpFrom, CpHere);
                        //if (MessageBox.Show("Issueing folder [" + CpHere + "] completed. Do you want to open it?", "Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        if (MessageBox.Show("Phát hành thư mục [" + CpHere + "] hoàn thành. Bạn có muốn mở thư mục?", "Hoàn thành", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(CpHere);
                        break;
                    }
                }//Ket thuc Duyet
            }
            catch { }
        }

        void MoveDirectory(string strSourceDir, string strDestDir)
        {
            if (Directory.Exists(strSourceDir))
            {
                try
                {
                    CopyDirectory(new DirectoryInfo(strSourceDir), new DirectoryInfo(strDestDir));
                }
                catch (Exception subEx)
                {
                    throw subEx;
                }
            }
        }

        void CopyDirectory(DirectoryInfo diSourceDir, DirectoryInfo diDestDir)
        {
            if (!diDestDir.Exists) diDestDir.Create();
            FileInfo[] fiSrcFiles = diSourceDir.GetFiles();
            foreach (FileInfo fiSrcFile in fiSrcFiles)
            {
                fiSrcFile.CopyTo(Path.Combine(diDestDir.FullName, fiSrcFile.Name));
            }
            DirectoryInfo[] diSrcDirectories = diSourceDir.GetDirectories();
            foreach (DirectoryInfo diSrcDirectory in diSrcDirectories)
            {
                CopyDirectory(diSrcDirectory, new DirectoryInfo(Path.Combine(diDestDir.FullName, diSrcDirectory.Name)));
            }
        }

        #endregion

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}